package com.hualan.sfzy.service.course.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.course.Course;
import com.hualan.sfzy.entity.course.CourseClassify;
import com.hualan.sfzy.mapper.course.CourseClassifyMapper;
import com.hualan.sfzy.mapper.course.CourseMapper;
import com.hualan.sfzy.service.course.CourseClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseClassifyServiceImpl implements CourseClassifyService {
    @Autowired
    private CourseClassifyMapper courseClassifyMapper;

    @Autowired
    private CourseMapper courseMapper;
    @Override
    public int add(CourseClassify courseClassify) {
        return courseClassifyMapper.insert(courseClassify);
    }

    @Override
    public int remove(Integer id) {
        return courseClassifyMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return courseClassifyMapper.deleteBatch(ids);
    }

    @Override
    public int edit(CourseClassify courseClassify) {
        return courseClassifyMapper.update(courseClassify);
    }

    @Override
    public CourseClassify findById(Integer id) {
        return courseClassifyMapper.selectById(id);
    }

    @Override
    public List<CourseClassify> find() {
        return courseClassifyMapper.select(null);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, CourseClassify courseClassify) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseClassify> list = courseClassifyMapper.select(courseClassify);
        PageInfo<CourseClassify> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    @Override
    public boolean hasCourses(Integer classifyId) {
        int count = courseMapper.countCoursesByClassifyId(classifyId);
        return count > 0;
    }
}